package com.google.code.mamule.user;

import com.google.code.mamule.integration.Message;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

/**
 * Created with IntelliJ IDEA. User: borck_000 ; Date: 12/19/13 ; Time: 2:20 PM
 */
@Component
@Path("/user")
@Produces(MediaType.APPLICATION_JSON)
@Transactional
public class UserEndpoint {

  private UserRepository users;
  private ActiveUser active;

  @Autowired
  public UserEndpoint(final UserRepository users, ActiveUser active) {
    this.users = users;
    this.active = active;
  }

  @GET
  public User fetch() {
    return active.get();
  }

  @PUT
  public Response update(User details) {
    User current = active.get();
    if (!current.getUsername().equals(details.getUsername())) {
      return Response
          .status(Response.Status.BAD_REQUEST)
          .entity(Message.error("username mismatch : expected %s", current.getUsername()))
          .build();
    }
    current.setEmail(details.getEmail());
    current.setIdentity(details.getIdentity());
    return Response.ok(Message.info("updated user %s", current.getUsername())).build();
  }

  @DELETE
  public Response unregister() {
    User current = active.get();
    users.delete(current.getUsername());
    return Response.ok(Message.info("unregistered %s", current.getUsername())).build();
  }

  @POST
  @Path("/register")
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  public Response register(@Context final UriInfo uri, @FormParam("username") String username,
                           @FormParam("password") String password) {
    User created = new User();
    created.setUsername(username);
    created.setPassword(password);
    return createIfNotExisting(uri, created);
  }

  private Response createIfNotExisting(final UriInfo uri, final User created) {
    if (users.exists(created.getUsername())) {
      return Response.status(Response.Status.CONFLICT)
          .entity(Message.error("user %s already exists", created.getUsername())).build();
    } else {
      users.save(created);
      return Response.created(uri.getBaseUriBuilder().path(this.getClass()).build()).build();
    }
  }

  @POST
  @Path("/register")
  @Consumes(MediaType.APPLICATION_JSON)
  public Response registerWithJson(@Context UriInfo uri, UserRegistration registration) {
    User created = new User();
    created.setUsername(registration.getUsername());
    created.setPassword(registration.getPassword());
    return createIfNotExisting(uri, created);
  }
}
